//7. RDD estimates
clear all
set more off
pause on
set trace off
cap log close
set matsize 5000
set maxvar 32767


	loc controls_rdd  winter spring summer fall
	
	*Seasonal Trend
		loc seasonXrunning
		loc x
			
		foreach control of loc controls_rdd {
			loc x `control'##c.running_var `control'##c.running_var2
			loc seasonXrunning `seasonXrunning' `x' 
		}


	foreach var in all_attacks     {

		use `"/Users/mateo/Dropbox/Land_Data_Analysis/WP_Replication/Datasets/RDD_`var'"', clear
				

			xi: areg as_attacks `seasonXrunning', cluster(codmpio) absorb(Año)
			predict resid, resid
			

					
		** Titled & Placebo Municipalities
	
			foreach x in 2 {
			
				preserve
					
					replace running_var=. if running_var==0
	 
					tempfile analysis
					save `analysis', replace
					
					** Title (Main Sample)
									
					lpoly resid running_var if (running_var>0 & running_var<10) & any_title==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se 
					drop if x==.
					tempfile RD
					save `RD', replace
					
					
					use `analysis', clear
					lpoly resid running_var  if (running_var>-10 & running_var<0) & any_title==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se 
					drop if x==.
					append using `RD'
					
					g ciplus=s+1.64*se
					g ciminus=s-1.64*se
					keep if abs(x)<10
					save `RD', replace
					
					
					*---generate bins for taking averages---*
					
					use `analysis', clear
					keep if abs(running_var)<10 & any_title==1
					
					gen bin1=.
					foreach X of num -10(1)10 {
						di "`X'"
						replace bin=-`X' if (running_var>=-`X' & running_var<(-`X'+1) & running_var<0)
						replace bin=`X' if (running_var>`X' & running_var<=(`X'+1))
					}
					tab bin1
					
					drop if bin1==.
					collapse resid running_var, by(bin1)
									
					append using `RD'
					
					tempfile title
					save `title', replace
					
					** No Title (Placebo)
					
					use `analysis', clear

					lpoly resid running_var if (running_var>0 & running_var<10) & any_title==0, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no
					ren x x_no
					ren se se_no
					drop if x==.
					tempfile RDno
					save `RDno', replace
					
					
					use `analysis', clear
					lpoly resid running_var  if (running_var>-10 & running_var<0) & any_title==0, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no
					ren x x_no
					ren se se_no
					drop if x==.
					append using `RDno'
					
					g ciplus_no=s_no+1.64*se
					g ciminus_no=s_no-1.64*se
					keep if abs(x)<10
					save `RDno', replace
					
					
					*---generate bins for taking averages---*
					
					use `analysis', clear
					keep if abs(running_var)<10 & any_title==0
					
					gen bin1=.
					foreach X of num -10(1)10 {
						di "`X'"
						replace bin=-`X' if (running_var>=-`X' & running_var<(-`X'+1) & running_var<0)
						replace bin=`X' if (running_var>`X' & running_var<=(`X'+1))
					}
					tab bin1
					
					drop if bin1==.
					collapse resid running_var, by(bin1)
					
					ren resid resid_no
					ren running_var running_var_no
									
					append using `RDno'
					
					append using `title'
					
					
					 *** Confidence Bands ***
					twoway (connected s x if x>0, sort msymbol(none) clcolor(black) clpat(solid) clwidth(medthick))  (connected s_no x_no if x>0, sort msymbol(none) clcolor(red) clpat(solid) clwidth(medthick))  /*
					*/(connected ciplus x if x>0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) /*
					*/(connected ciminus x if x>0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) /*
					*/(connected s x if x<0, sort msymbol(none) clcolor(black) clpat(solid) clwidth(medthick)) (connected s_no x_no if x<0, sort msymbol(none) clcolor(red) clpat(solid) clwidth(medthick)) /*
					*/(connected ciplus x if x<0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) /*
					*/(connected ciminus x if x<0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) /*
					*/ (scatter resid running_var, sort msize(small) mcolor(black)) (scatter resid_no running_var_no,  sort msize(small) mcolor(red) msymbol(triangle)) , /*
					*/legend(order(5 6) label (5 "Municipalities titled") label (6 "Municipalities {bf:not} titled" "({it:placebo})") ring(0) row(2) pos(10) region(color(none))) /*
					*/ /*legend(off)*/ graphregion(color(white)) /*
					*/ ytitle("All Attacks") xlabel(-10(2)10) xsc(r(-10 10)) ylabel(0(0.01)0.03) ysc(r(0 .03)) xtitle(Normalized Week) ylab(,nogrid) /*
					*/xline(0, lpattern(line) lwidth(thin) lc(black))/*
					*/ylabel(,angle(360)) subtitle("                	  {&larr}weeks {bf:before} title               weeks {bf:after} title {&rarr}                            ")
					
					graph export `"/Users/mateo/Desktop/RDD_`var'.png"', replace
						
				restore			
			}		
	}
	
